package com.moji.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.common.utils.DateUtils;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjHomeProduct;
import com.moji.entity.MjHomeSolution;
import com.moji.entity.MjNews;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjHomeSolutionMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjHomeSolutionService;
import com.moji.util.XMLAppendExample;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 首页解决方案详情(MjHomeSolution)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:20
 */
@Service("mjHomeSolutionService")
public class MjHomeSolutionServiceImpl extends ServiceImpl<MjHomeSolutionMapper, MjHomeSolution> implements MjHomeSolutionService {
    @Resource
    private MjHomeSolutionMapper mjHomeSolutionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjHomeSolution queryById(Long id) {
        return this.mjHomeSolutionDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param mjHomeSolution 筛选条件
     * @return 查询结果
     */
    @Override
    public List<MjHomeSolution> queryByPage(MjHomeSolution mjHomeSolution) {
        LambdaQueryWrapper<MjHomeSolution> queryWrapper = new LambdaQueryWrapper<>();
        if (mjHomeSolution.getSolutionId() != null) {
            queryWrapper.eq(MjHomeSolution::getSolutionId, mjHomeSolution.getSolutionId());
        }
        if (mjHomeSolution.getType() != null) {
            queryWrapper.eq(MjHomeSolution::getType, mjHomeSolution.getType());
        }
        queryWrapper.orderByAsc(MjHomeSolution::getSort);
        queryWrapper.orderByDesc(MjHomeSolution::getUpdateTime);
        List<MjHomeSolution> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjHomeSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeSolution insert(MjHomeSolution mjHomeSolution) {
        mjHomeSolution.setCreateTime(new Date());
        mjHomeSolution.setUpdateTime(new Date());
        mjHomeSolution.setTypeTitle("其它");
        LambdaQueryWrapper<MjHomeSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(MjHomeSolution::getTypeSubtitle);
        List<MjHomeSolution> list = this.list(queryWrapper);
        if(ObjectUtil.isNotEmpty(list)){
            mjHomeSolution.setTypeSubtitle(list.get(0).getTypeSubtitle());
        }
        this.mjHomeSolutionDao.insert(mjHomeSolution);
        return mjHomeSolution;
    }

    /**
     * 修改数据
     *
     * @param mjHomeSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeSolution update(MjHomeSolution mjHomeSolution) {
        mjHomeSolution.setUpdateTime(new Date());
        LambdaQueryWrapper<MjHomeSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjHomeSolution::getId,mjHomeSolution.getId());
        this.mjHomeSolutionDao.update(mjHomeSolution,queryWrapper);
        return this.queryById(mjHomeSolution.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjHomeSolutionDao.deleteById(id) > 0;
    }
}
